<template>
  <div>
    <button @click="openDahuaServe">打开大华服务</button>
    <button @click="initDahuaWs">发送大华心跳消息</button>
    <button @click="ReadICCard">去读ic卡号</button>
    <button @click="readCPUCard">去读cpu卡号</button>
    <button @click="closeSever">关闭服务</button>
  </div>
</template>

<script>
import WebSocketComm from "@/unit/webSocketComm";

export default {
  name: "dahua",
  data() {
    return {
      ws1: null,
      ws2: null,
    };
  },
  created() {
    // this.openWenDouServe();
    // this.openDahuaServe();
  },
  methods: {
    openWenDouServe() {
      const data = {
        url: "ws://127.0.0.1:22225",
      };
      this.ws1 = new WebSocketComm(data);
      this.ws1.openSocket();
    },

    openDahuaServe() {
      window.location.href = 'DHLocalServer://';
      const data = {
        url: "ws://localhost:12000/",
        wsHeartData: {
          id: 1,
          session: 0,
          method: "common.heartbeat",
          info: {},
        },
        heartbeatTimer: 10
      };
      this.ws2 = new WebSocketComm(data);
      this.ws2.openSocket();
      // 如果连接成功
      this.ws2.addEventLister((e) => {
        this.getMsg(e);
      });
    },

    // 发送心跳
    initDahuaWs() {
      const data = {
        id: 1,
        session: 0,
        method: "common.heartbeat",
        info: {},
      };
      this.sendMsg(data);
    },

    ReadICCard(){
      let msg = {"id": 1, "session": 0, "method": "card.read","info": {"cardType":1}};
      this.sendMsg(msg);
    },

    // 去读cpu卡
    readCPUCard() {
      const data = {
        method: "card.read",
        // info: {
        //   RFIDCardType: 0,
        //   cardType: 2,
        //   CPUCardType: 1,
        //   CPUPassword: "",
        //   CPUSlot: 0,
        //   CPUEncry: 0,
        //   elm: "",
        // },
        info: {
          // 卡类型（1-id卡，2-cpu卡，3-rfid卡）
          'cardType': 2,
          // IC 读卡器类型（1-大华读卡器;2-邦融指纹发卡一体机）
          'ICCardType': 1,
          // CPU 卡细分类型（1-华虹cpu 卡;2-国密 cpu）
          'CPUCardType': 2,
          // RFID卡细分类型（1-有源RFID;2-定位 RFID）
          // 'RFIDCardType': 1,
          // CPU 卡密码
          'CPUPassword': "",
          // CPU 卡槽（int）
          'CPUSlot': 2,
          // CPU 卡是否加密(int)
          'CPUEncry': 0,
          // 
          // 'elm': "",
        },
        id: 2,
        session: 0,
      };
      this.sendMsg(data);
    },

    // 发送消息
    sendMsg(msg) {
      const data = JSON.stringify(msg);
      this.ws2.wsSend(data);
    },

    // 接收消息
    getMsg(e) {
      const data = JSON.parse(e.data.replace(/[\r\n]/g, ""));
      // console.log(data)
      if (data.success) {
        // 接收成功的逻辑
        // console.log(data.method);
        if(data.method == 'common.heartbeat'){
          return;
        }

        // if()
      }
    },

    // 关闭服务
    closeSever() {
      const data = {
        id: 1,
        session: 0,
        method: "common.quitApp",
        info: {},
      };
      this.sendMsg(data);
      this.ws2.wsClose();
    },
  },
};
</script>

<style>
</style>